package com.deyang.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.deyang.common.core.text.Convert;
import com.deyang.common.utils.DateUtils;
import com.deyang.common.utils.POIUtil;
import com.deyang.system.domain.ResidentRegistInfo;
import com.deyang.system.domain.vo.ResidentRegistVo;
import com.deyang.system.mapper.ResidentRegistInfoMapper;
import com.deyang.system.service.IResidentRegistInfoService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 居民登记信息Service业务层处理
 * 
 * @author dingqianbin
 * @date 2021-08-06
 */
@Service
public class ResidentRegistInfoServiceImpl implements IResidentRegistInfoService
{

    @Autowired
    private ResidentRegistInfoMapper residentRegistInfoMapper;

    /**
     * 查询居民登记信息
     * 
     * @param id 居民登记信息主键
     * @return 居民登记信息
     */
    @Override
    public ResidentRegistInfo selectResidentRegistInfoById(String id)
    {
        return residentRegistInfoMapper.selectResidentRegistInfoById(id);
    }

    /**
     * 查询居民登记信息列表
     * 
     * @param residentRegistInfo 居民登记信息
     * @return 居民登记信息
     */
    @Override
    public List<ResidentRegistInfo> selectResidentRegistInfoList(ResidentRegistInfo residentRegistInfo)
    {
        return residentRegistInfoMapper.selectResidentRegistInfoList(residentRegistInfo);
    }

    /**
     * 新增居民登记信息
     * 
     * @param residentRegistInfo 居民登记信息
     * @return 结果
     */
    @Override
    public int insertResidentRegistInfo(ResidentRegistInfo residentRegistInfo)
    {
        residentRegistInfo.setCreateTime(DateUtils.getNowDate());
        return residentRegistInfoMapper.insertResidentRegistInfo(residentRegistInfo);
    }

    /**
     * 修改居民登记信息
     * 
     * @param residentRegistInfo 居民登记信息
     * @return 结果
     */
    @Override
    public int updateResidentRegistInfo(ResidentRegistInfo residentRegistInfo)
    {
        residentRegistInfo.setUpdateTime(DateUtils.getNowDate());
        return residentRegistInfoMapper.updateResidentRegistInfo(residentRegistInfo);
    }

    /**
     * 批量删除居民登记信息
     * 
     * @param ids 需要删除的居民登记信息主键
     * @return 结果
     */
    @Override
    public int deleteResidentRegistInfoByIds(String ids)
    {
        return residentRegistInfoMapper.deleteResidentRegistInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除居民登记信息信息
     * 
     * @param id 居民登记信息主键
     * @return 结果
     */
    @Override
    public int deleteResidentRegistInfoById(String id)
    {
        return residentRegistInfoMapper.deleteResidentRegistInfoById(id);
    }

    @Override
    public List<ResidentRegistInfo> selectResidentRegistList(ResidentRegistVo residentRegistVo) {
        return residentRegistInfoMapper.selectResidentRegistList(residentRegistVo);
    }


    @Override
    public Map<String,Object> selectCommunity(){
        Map<String,Object> map = new HashMap<>();
        map.put("overseas",residentRegistInfoMapper.selectCommunity1().get("overseas"));
        map.put("keyCities",residentRegistInfoMapper.selectCommunity2().get("keyCities"));
        map.put("inRisk",residentRegistInfoMapper.selectCommunity3().get("inRisk"));
        map.put("close",residentRegistInfoMapper.selectCommunity4().get("close"));
        map.put("home",residentRegistInfoMapper.selectCommunity5().get("home"));
        map.put("concentrated",residentRegistInfoMapper.selectCommunity6().get("concentrated"));
        return map;
    }



    @Override
    public List<ResidentRegistInfo> listCommunity(String state){
        HashMap<String,Object> map = new HashMap<>();
        map.put("state",state);
        return residentRegistInfoMapper.listCommunity(map);
    }
    /**
     * 数据导出
     */
    @Override
    public HSSFWorkbook export() {
        //excel标题
        String[] title = {"序号", "姓名", "性别", "年龄", "住址(在德阳常住地)", "身份证号", "联系电话", "返回旌阳时间", "社区", "返回省市区", "采核酸时间", "备注"};
        //sheet名
        String sheetName = "重点地区返回人员健康监测台账";
        ResidentRegistInfo residentRegistInfo =new ResidentRegistInfo();
        List<ResidentRegistInfo> projectMap = this.selectResidentRegistInfoList(residentRegistInfo);
        List<Object> list = null;
        String[][] cont = new String[projectMap.size()][];
        try {
            for (int i = 0, l = projectMap.size(); i < l; i++) {
                ResidentRegistInfo residentRegistInfo1 = projectMap.get(i);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString1 = formatter.format(residentRegistInfo1.getReturnTime());
                String dateString2 = formatter.format(residentRegistInfo1.getAcquisitionTime());
                cont[i] = new String[title.length];
                cont[i][0] = "" + (i + 1);
                cont[i][1] = (String) residentRegistInfo1.getName();
                cont[i][2] = (String) residentRegistInfo1.getSex();
                cont[i][3] = (String) residentRegistInfo1.getAge().toString();
                cont[i][4] = (String) residentRegistInfo1.getAddress();
                cont[i][5] = (String) residentRegistInfo1.getCardNo();
                cont[i][6] = (String) residentRegistInfo1.getPhone();
                cont[i][7] = dateString1;
                cont[i][8] = (String) residentRegistInfo1.getCommunity();
                cont[i][9] = (String) residentRegistInfo1.getCity();
                cont[i][10] = dateString2;
                cont[i][11] = (String) residentRegistInfo1.getRemarke();
            }
        } catch (Exception e) {
        }
        HSSFWorkbook hssfWorkbook = POIUtil.getHSSFWorkbook(sheetName, title, cont, null);
        return hssfWorkbook;
    }
}
